<!DOCTYPE html><html lang="zh-CN" data-theme="dark"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><title>微信公众号配置 | 北城韩雨</title><meta name="keywords" content="微信"><meta name="author" content="北城韩雨"><meta name="copyright" content="北城韩雨"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#0d0d0d"><meta name="description" content="微信">
<meta property="og:type" content="article">
<meta property="og:title" content="微信公众号配置">
<meta property="og:url" content="http://example.com/2020/04/20/%E5%85%AC%E4%BC%97%E5%8F%B7%E9%85%8D%E7%BD%AE/index.html">
<meta property="og:site_name" content="北城韩雨">
<meta property="og:description" content="微信">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2018-07-17%2Faf41dabad727ba80c9ca8a2bfbd22a58%2F00004.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&refer=http%3A%2F%2Fpic.vjshi.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1629530659&t=bbcfe1e86f393eecbb0722db1b92e475">
<meta property="article:published_time" content="2020-04-19T16:00:00.000Z">
<meta property="article:modified_time" content="2019-06-02T16:00:00.000Z">
<meta property="article:author" content="北城韩雨">
<meta property="article:tag" content="微信公众号配置">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2018-07-17%2Faf41dabad727ba80c9ca8a2bfbd22a58%2F00004.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&refer=http%3A%2F%2Fpic.vjshi.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1629530659&t=bbcfe1e86f393eecbb0722db1b92e475"><link rel="shortcut icon" href="/img/favicon.png"><link rel="canonical" href="http://example.com/2020/04/20/%E5%85%AC%E4%BC%97%E5%8F%B7%E9%85%8D%E7%BD%AE/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: undefined,
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'mediumZoom',
  Snackbar: undefined,
  source: {
    jQuery: 'https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js',
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/js/jquery.justifiedGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/css/justifiedGallery.min.css'
    },
    fancybox: {
      js: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js',
      css: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isanchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: '微信公众号配置',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2019-06-03 00:00:00'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
    const detectApple = () => {
      if (GLOBAL_CONFIG_SITE.isHome && /iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><link rel="stylesheet" href="/css/custom.css"  media="defer" onload="this.media='all'"><link rel="stylesheet" href="/css/coin.css"  media="defer" onload="this.media='all'"><link rel="stylesheet" href="/css/liuxing.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/l-lin/font-awesome-animation/dist/font-awesome-animation.min.css"  media="defer" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/swiper/swiper-bundle.min.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/zhheo/JS-Heo@main/bb/showbb_in_index.css"><!-- hexo injector head_end start --><link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/4.1.1/animate.min.css" media="print" onload="this.media='screen'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/l-lin/font-awesome-animation/dist/font-awesome-animation.min.css" media="defer" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/hexo-butterfly-tag-plugins-plus/lib/tag_plugins.min.css" media="defer" onload="this.media='all'"><!-- hexo injector head_end end --><meta name="generator" content="Hexo 5.3.0"></head><body><div id="web_bg"></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="/img/my.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data"><div class="data-item is-center"><div class="data-item-link"><a href="/archives/"><div class="headline">文章</div><div class="length-num">65</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/tags/"><div class="headline">标签</div><div class="length-num">23</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/categories/"><div class="headline">分类</div><div class="length-num">21</div></a></div></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/"><i class="fa-fw fas fa-home faa-tada"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/shuoshuo/"><i class="fa-fw fas fa-archive faa-tada"></i><span> 叨逼叨</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/tags/"><i class="fa-fw fas fa-tags faa-tada"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/categories/"><i class="fa-fw fas fa-folder-open faa-tada"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/link/"><i class="fa-fw fas fa-link faa-tada"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/comments/"><svg class="icon faa-tada" aria-hidden="true"><use xlink:href="#icon-message"></use></svg><span> 留言板</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/about/"><i class="fa-fw fas fa-heart faa-tada"></i><span> 个人简介</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2018-07-17%2Faf41dabad727ba80c9ca8a2bfbd22a58%2F00004.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&amp;refer=http%3A%2F%2Fpic.vjshi.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1629530659&amp;t=bbcfe1e86f393eecbb0722db1b92e475')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">北城韩雨</a></span><div id="menus"><div class="menus_items"><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/"><i class="fa-fw fas fa-home faa-tada"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/shuoshuo/"><i class="fa-fw fas fa-archive faa-tada"></i><span> 叨逼叨</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/tags/"><i class="fa-fw fas fa-tags faa-tada"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/categories/"><i class="fa-fw fas fa-folder-open faa-tada"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/link/"><i class="fa-fw fas fa-link faa-tada"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/comments/"><svg class="icon faa-tada" aria-hidden="true"><use xlink:href="#icon-message"></use></svg><span> 留言板</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/about/"><i class="fa-fw fas fa-heart faa-tada"></i><span> 个人简介</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">微信公众号配置</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2020-04-19T16:00:00.000Z" title="发表于 2020-04-20 00:00:00">2020-04-20</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2019-06-02T16:00:00.000Z" title="更新于 2019-06-03 00:00:00">2019-06-03</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E5%BE%AE%E4%BF%A1/">微信</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">3.2k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>11分钟</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="微信公众号配置"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h1 id="公众号服务端"><a href="#公众号服务端" class="headerlink" title="公众号服务端"></a>公众号服务端</h1><h2 id="1-本地端口映射公网IP"><a href="#1-本地端口映射公网IP" class="headerlink" title="1.本地端口映射公网IP"></a>1.本地端口映射公网IP</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>使用工具 https:<span class="comment">//www.ngrok.cc/  ngrok</span></span><br><span class="line"><span class="number">2.</span>首先登录 然后开通隧道 把你本机的服务器地址填写进去 找到隧道管理 获取ID</span><br><span class="line"><span class="number">3.</span>启动下载好的客户端 把ID粘贴进去即可</span><br></pre></td></tr></table></figure>
<h2 id="2-公众号测试配置"><a href="#2-公众号测试配置" class="headerlink" title="2.公众号测试配置"></a>2.公众号测试配置</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>登录以后 点击开发者工具 -&gt; 公众平台测试账号</span><br><span class="line"><span class="number">2.</span>这里会受到你的appid 和 appsecret 用于配置</span><br><span class="line"><span class="number">3.</span>还有接口配置和token 这里也是用于配置</span><br></pre></td></tr></table></figure>
<h2 id="3-服务器之间交互"><a href="#3-服务器之间交互" class="headerlink" title="3.服务器之间交互"></a>3.服务器之间交互</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>这里我们用关键词回复举例 首先当有人给我们的公众号发消息的时候我们在微信平台的后台 可以设置自动的关键词回复</span><br><span class="line"><span class="number">2.</span>当有人给我们的公众号发送消息的时候 是发送到微信的服务器上 我们可以通过配置 把他发到我们自己的服务器上 然后服务器的代码会根据判断 在去回复关键词 在发送给微信服务器 微信服务器在发给客户 这是一个基本陵城</span><br><span class="line"><span class="number">3.</span>以上操作需要配置 这里我们使用co-wachat的包去完成</span><br><span class="line"><span class="number">4.</span>这一步的流程(配ID 配token)都是我们要验证微信 因为网络上那么多请求 我怎么知道 你是不是微信 </span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//配置 首先在我我们的代码里需要配置你的微信公众号的appid和appappsecret  当我们申请测试账号的时候 会有 </span></span><br><span class="line"><span class="comment">//conf.js文件</span></span><br><span class="line"><span class="built_in">module</span>.exports = &#123;</span><br><span class="line">    <span class="comment">//平台获取</span></span><br><span class="line">    appid:<span class="string">&#x27;wx84cf5ad48c0a3257&#x27;</span>,</span><br><span class="line">    <span class="comment">//平台获取</span></span><br><span class="line">    appsecret:<span class="string">&#x27;c7435a3e4446c5dd2863709b9d1bde18&#x27;</span>,</span><br><span class="line">    <span class="comment">//自定义</span></span><br><span class="line">    token:<span class="string">&#x27;wangzhenglike&#x27;</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//我们需要在微信平台的后台 把接口配置到我们的服务器 这个时候就需要我们上面提到的本地端口映射公网IP的软件</span></span><br><span class="line"><span class="comment">//下面是微信平台的测试账号页面的 一些配置 </span></span><br><span class="line">接口配置信息修改</span><br><span class="line">请填写接口配置信息，此信息需要你有自己的服务器资源，填写的URL需要正确响应微信发送的Token验证，请阅读消息接口使用指南。</span><br><span class="line"><span class="comment">//这里是我们的映射的公网IP</span></span><br><span class="line">URL</span><br><span class="line">http:<span class="comment">//beichenghanyu.free.idcfengye.com/wechat</span></span><br><span class="line"><span class="comment">//自定义的token和上面我们写的保持一致</span></span><br><span class="line">Token</span><br><span class="line">wangzhenglike</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//代码</span></span><br><span class="line"><span class="keyword">const</span> Koa = <span class="built_in">require</span>(<span class="string">&#x27;koa&#x27;</span>)</span><br><span class="line"><span class="keyword">const</span> Router = <span class="built_in">require</span>(<span class="string">&#x27;koa-router&#x27;</span>)</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">static</span> = <span class="built_in">require</span>(<span class="string">&#x27;koa-static&#x27;</span>)</span><br><span class="line"><span class="keyword">const</span> bodyParser = <span class="built_in">require</span>(<span class="string">&#x27;koa-bodyparser&#x27;</span>);</span><br><span class="line"><span class="keyword">const</span> app = <span class="keyword">new</span> Koa()</span><br><span class="line"><span class="keyword">const</span> conf = <span class="built_in">require</span>(<span class="string">&#x27;./conf&#x27;</span>)</span><br><span class="line">app.use(bodyParser())</span><br><span class="line"><span class="keyword">const</span> router = <span class="keyword">new</span> Router()</span><br><span class="line">app.use(<span class="keyword">static</span>(__dirname + <span class="string">&#x27;/&#x27;</span>))</span><br><span class="line"><span class="comment">//加载co-wechat的包</span></span><br><span class="line"><span class="keyword">const</span> wechat = <span class="built_in">require</span>(<span class="string">&#x27;co-wechat&#x27;</span>)</span><br><span class="line"><span class="comment">//这里就是请求什么的路由的时候 在我们的微信平台后台的接口里配置过  </span></span><br><span class="line">router.all(<span class="string">&#x27;/wechat&#x27;</span>, wechat(conf).middleware(</span><br><span class="line">    <span class="keyword">async</span> message =&gt; &#123;</span><br><span class="line">        <span class="comment">//这里message.Content就是客户给我们法国来的消息</span></span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">&#x27;wechat:&#x27;</span>, message)</span><br><span class="line">        <span class="comment">//下面是我们的回复的关键字</span></span><br><span class="line">        <span class="keyword">return</span> <span class="string">&#x27;Hello World &#x27;</span> + message.Content</span><br><span class="line">    &#125;</span><br><span class="line">))</span><br><span class="line">app.use(router.routes()); <span class="comment">/*启动路由*/</span></span><br><span class="line">app.use(router.allowedMethods());</span><br><span class="line">app.listen(<span class="number">3000</span>);</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//通过扫描测试号二维码 即可测试  </span></span><br></pre></td></tr></table></figure>
<h2 id="4-服务器API调用"><a href="#4-服务器API调用" class="headerlink" title="4.服务器API调用"></a>4.服务器API调用</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>就是我们自己服务器需要调用 微信公众号后台的那写接口 比如他功能里面的那写 基础配置 像自定义菜单 留言管理 页面模板之类的</span><br><span class="line"><span class="number">2.</span>调用微信的API接口我们需要使用co-wechat-api的第三方包  这里是为了方便获取权限用的</span><br><span class="line"><span class="number">3.</span>我们要掉微信的服务器的接口 肯定是微信验证我们 所以 我们第一步就是需要从微信上面获取access_token  具体查看官方文档接口通过传入给微信服务器传入我们的appid 和 appsecret 获取access_token了</span><br><span class="line"><span class="number">4.</span>下面我们方便测试用前端举例</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//下面是我们的后端代码  前面是有一个页面发送请求的 这里我们为了方便就不写代码了 </span></span><br><span class="line"><span class="keyword">const</span> Koa = <span class="built_in">require</span>(<span class="string">&#x27;koa&#x27;</span>)</span><br><span class="line"><span class="keyword">const</span> Router = <span class="built_in">require</span>(<span class="string">&#x27;koa-router&#x27;</span>)</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">static</span> = <span class="built_in">require</span>(<span class="string">&#x27;koa-static&#x27;</span>)</span><br><span class="line"><span class="keyword">const</span> bodyParser = <span class="built_in">require</span>(<span class="string">&#x27;koa-bodyparser&#x27;</span>);</span><br><span class="line"><span class="keyword">const</span> axios = <span class="built_in">require</span>(<span class="string">&#x27;axios&#x27;</span>)</span><br><span class="line"><span class="keyword">const</span> app = <span class="keyword">new</span> Koa()</span><br><span class="line"><span class="keyword">const</span> conf = <span class="built_in">require</span>(<span class="string">&#x27;./conf&#x27;</span>)</span><br><span class="line">app.use(bodyParser())</span><br><span class="line"><span class="keyword">const</span> router = <span class="keyword">new</span> Router()</span><br><span class="line">app.use(<span class="keyword">static</span>(__dirname + <span class="string">&#x27;/&#x27;</span>))</span><br><span class="line"></span><br><span class="line"><span class="comment">//接受前端发过来的请求之后 我们就把我们的和微信服务器之间的接口填写的ID 和 secret写好</span></span><br><span class="line">router.get(<span class="string">&#x27;/getTokens&#x27;</span>,<span class="keyword">async</span> () =&gt; &#123;</span><br><span class="line">    <span class="keyword">const</span> url = <span class="string">` https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&amp;appid=<span class="subst">$&#123;conf.appid&#125;</span>&amp;secret=<span class="subst">$&#123;conf.appsecret&#125;</span>`</span></span><br><span class="line">    <span class="comment">//给微信服务器发送请求 获取了 access_token token</span></span><br><span class="line">    <span class="keyword">const</span> &#123;data&#125; = <span class="keyword">await</span> axios.get(url)</span><br><span class="line">    <span class="built_in">console</span>.log(data,<span class="string">&#x27;----我是拉去的token&#x27;</span>)</span><br><span class="line">    <span class="comment">//然后带着access_token 在去给微信服务器发请求获取订阅号的关注的用户列表</span></span><br><span class="line">    <span class="keyword">const</span> getFollerUrl = <span class="string">`https://api.weixin.qq.com/cgi-bin/user/get?access_token=<span class="subst">$&#123;data.access_token&#125;</span>`</span></span><br><span class="line">    <span class="keyword">const</span> ret  = <span class="keyword">await</span> axios.get(getFollerUrl)</span><br><span class="line">    <span class="built_in">console</span>.log(ret.data,<span class="string">&#x27;-----我是获取列表&#x27;</span>)</span><br><span class="line">&#125;)</span><br><span class="line">app.use(router.routes()); <span class="comment">/*启动路由*/</span></span><br><span class="line">app.use(router.allowedMethods());</span><br><span class="line">app.listen(<span class="number">3000</span>);</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//但是上面的操作很麻烦 我们要主动点击前端的按钮才能 获取access_token 还需要有前端页面的辅助 所以 这我们就使用了co-wechat-app第三放包  这里 他把我们所有的接口都封装好了 我们只需要调用方法即可</span></span><br><span class="line"><span class="keyword">const</span> WechatAPI = <span class="built_in">require</span>(<span class="string">&#x27;co-wechat-api&#x27;</span>)</span><br><span class="line"><span class="keyword">const</span> api = <span class="keyword">new</span> WechatAPI(conf.appid,conf.appsecret)</span><br><span class="line"><span class="comment">//前端点击获取用户按钮 后端接收到前端请求 然后 调用我们上面的方法即可</span></span><br><span class="line">router.get(<span class="string">&#x27;/getFollowers&#x27;</span>,<span class="keyword">async</span> ctx =&gt; &#123;</span><br><span class="line">    <span class="comment">//这样就直接调用第三包封装好了的接口了</span></span><br><span class="line">    <span class="keyword">let</span> ret = <span class="keyword">await</span> api.getFollowers()</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
<h2 id="5-全局票据"><a href="#5-全局票据" class="headerlink" title="5.全局票据"></a>5.全局票据</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>简单来说我们后端代码跑在ng服务器上 不可能只有一个后端的的节点代码 如果其中一个挂掉 你的access_token怎么办</span><br><span class="line"><span class="number">2.</span>所以这里要存放在mongoose里</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//模型文件</span></span><br><span class="line"><span class="keyword">const</span> mongoose = <span class="built_in">require</span>(<span class="string">&#x27;mongoose&#x27;</span>)</span><br><span class="line"><span class="keyword">const</span> &#123;</span><br><span class="line">    Schema</span><br><span class="line">&#125; = mongoose</span><br><span class="line"><span class="comment">//链接mongoDB</span></span><br><span class="line">mongoose.connect(<span class="string">&#x27;mongodb://localhost:27017/weixin&#x27;</span>, &#123;</span><br><span class="line">    useNewUrlParser: <span class="literal">true</span></span><br><span class="line">&#125;, <span class="function">() =&gt;</span> &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&#x27;Mongodb connected..&#x27;</span>)</span><br><span class="line">&#125;)</span><br><span class="line"><span class="comment">//建立字段模型</span></span><br><span class="line"><span class="built_in">exports</span>.ServerToken = mongoose.model(<span class="string">&#x27;ServerToken&#x27;</span>, &#123;</span><br><span class="line">    accessToken: <span class="built_in">String</span></span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//业务代码</span></span><br><span class="line"><span class="keyword">const</span> &#123; ServerToken &#125; = <span class="built_in">require</span>(<span class="string">&#x27;./mongoose&#x27;</span>)</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> WechatAPI = <span class="built_in">require</span>(<span class="string">&#x27;co-wechat-api&#x27;</span>)</span><br><span class="line"><span class="comment">//下面会有两个函数 第一个是当你第二次进来的时候 取token 第二个是当你第一次进来的时候存token</span></span><br><span class="line"><span class="comment">//这样就把token存在了全局的数据库里 </span></span><br><span class="line"><span class="keyword">const</span> api = <span class="keyword">new</span> WechatAPI(</span><br><span class="line">    conf.appid,</span><br><span class="line">    conf.appsecret,</span><br><span class="line">    <span class="comment">// 取Token</span></span><br><span class="line">    <span class="keyword">async</span> () =&gt; <span class="keyword">await</span> ServerToken.findOne(),</span><br><span class="line">    <span class="comment">// 存Token</span></span><br><span class="line">    <span class="keyword">async</span> token =&gt; <span class="keyword">await</span> ServerToken.updateOne(&#123;&#125;, token, &#123; <span class="attr">upsert</span>: <span class="literal">true</span> &#125;)</span><br><span class="line">)</span><br><span class="line">router.get(<span class="string">&#x27;/getFollowers&#x27;</span>, <span class="keyword">async</span> ctx =&gt; &#123;</span><br><span class="line">    <span class="keyword">let</span> res = <span class="keyword">await</span> api.getFollowers()</span><br><span class="line">    res = <span class="keyword">await</span> api.batchGetUsers(res.data.openid, <span class="string">&#x27;zh_CN&#x27;</span>)</span><br><span class="line">    ctx.body = res</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
<h1 id="公众号网页端"><a href="#公众号网页端" class="headerlink" title="公众号网页端"></a>公众号网页端</h1><h2 id="1-OAuth2授权"><a href="#1-OAuth2授权" class="headerlink" title="1.OAuth2授权"></a>1.OAuth2授权</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>OAuth2<span class="number">.0</span>授权 </span><br><span class="line"><span class="number">2.</span>首先需要在我们公众号平台的后台 配置一下 JS接口安全域名 和 网页账号中的域名  这里就直接粘贴我们公网的服务器IP地址接口 切记不用夹http</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//第三方登录 当我们登录到一个网站的时候  我们希望使用微信登录 就要通过OAuth授权码去做 </span></span><br><span class="line"><span class="number">1.</span>网页端向我们自己的服务器发起第三方登录请求 </span><br><span class="line"><span class="number">2.</span>然后我们自己的服务器会给他重定向到微信的登录界面</span><br><span class="line"><span class="number">3.</span>然后网页端向微信服务器发起登录请求</span><br><span class="line"><span class="number">4.</span>微信服务器收到了网页端的请求 回想我们自己都服务器发送一个code认证码</span><br><span class="line"><span class="number">5.</span>我们自己的服务器收到code码之后去向微信服务器申请令牌</span><br><span class="line"><span class="number">6.</span>微信服务器收到申请之后 会返回给我们自己的服务器令牌</span><br><span class="line"><span class="number">7.</span>我们自己的服务器收到令牌之后 然后刷新前端页面 登录成功</span><br><span class="line"><span class="number">8.</span>这里面在重定向的时候 需要用到co-wachat-oauth的库</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> OAuth = <span class="built_in">require</span>(<span class="string">&#x27;co-wechat-oauth&#x27;</span>)</span><br><span class="line"><span class="keyword">const</span> oauth = <span class="keyword">new</span> OAuth(conf.appid,conf.appsecret)</span><br><span class="line"><span class="comment">//网页端向我们服务器发起第三放登录 对应上面1步骤</span></span><br><span class="line">router.get(<span class="string">&#x27;/wxAuthorize&#x27;</span>,<span class="function">(<span class="params">ctx,next</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&#x27;/wxAuthorize的请求&#x27;</span>)</span><br><span class="line">    <span class="keyword">const</span> state = ctx.query.id</span><br><span class="line">    <span class="comment">//这里是前端完整的网址</span></span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&#x27;ctx....&#x27;</span>,ctx.href)</span><br><span class="line">    <span class="keyword">let</span> redirectUrl = ctx.href</span><br><span class="line">    <span class="comment">//这里我们服务器在向微信服务器发送请求的时候带着我们自己的一个表示 替换一下前端的所有网址 </span></span><br><span class="line">    redirectUrl = redirectUrl.replace(<span class="string">&#x27;wxAuthorize&#x27;</span>,<span class="string">&#x27;wxCallback&#x27;</span>)</span><br><span class="line">    <span class="built_in">console</span>.log(redirectUrl,<span class="string">&#x27;redirectUrl&#x27;</span>)</span><br><span class="line">    <span class="comment">//这里就相当于一个密钥  这里我们使用的是有感知的 也就是需要用户点同意登录</span></span><br><span class="line">    <span class="keyword">const</span> scope =<span class="string">&#x27;snsapi_userinfo&#x27;</span></span><br><span class="line">    <span class="comment">//然后这里生成微信的同意的登录界面</span></span><br><span class="line">    <span class="keyword">const</span> url = oauth.getAuthorizeURL(redirectUrl,state,scope)</span><br><span class="line">    <span class="built_in">console</span>.log(url,<span class="string">&#x27;-----url&#x27;</span>)</span><br><span class="line">    <span class="comment">//返回给前端让前端去点同意登录 对应的是上面2步骤 这里就会带着微信服务器给我们的验证码 我们需要拿着这个验证码去微信服务器申请令牌</span></span><br><span class="line">    ctx.redirect(url)</span><br><span class="line">&#125;)</span><br><span class="line">router.get(<span class="string">&#x27;/wxCallback&#x27;</span>,<span class="keyword">async</span>(ctx,next) =&gt; &#123;</span><br><span class="line">    <span class="keyword">const</span> code = ctx.query.code</span><br><span class="line">    <span class="comment">//我们自己的服务器带这验证码 去向微信服务器发送申请 带着验证码 对应5</span></span><br><span class="line">    <span class="keyword">const</span> token = <span class="keyword">await</span> oauth.getAccessToken(code)</span><br><span class="line">    <span class="comment">//这里微信服务器给我们返回了令牌 而且 还给我们返回了微信登录用户的ID</span></span><br><span class="line">    <span class="keyword">const</span> accessToken = token.data.access_token <span class="comment">//主要用于掉接口</span></span><br><span class="line">    <span class="keyword">const</span> openid = token.data.openid <span class="comment">//用户的唯一标识 可以用来做支付</span></span><br><span class="line">    <span class="comment">//这里我们带着微信服务返回给我们的用户ID 去跳转到别的页面 这里只是为了测试方便 当然 你可以做别的事</span></span><br><span class="line">    ctx.redirect(<span class="string">&#x27;/?opind=&#x27;</span> + openid)</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&#x27;wx call back&#x27;</span>)</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
<h2 id="2-获取用户信息"><a href="#2-获取用户信息" class="headerlink" title="2.获取用户信息"></a>2.获取用户信息</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//通过上面的操作我们已经可以拿到微信用的ID 之类的东西</span></span><br><span class="line"> <span class="keyword">async</span> <span class="function"><span class="title">getUser</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="comment">//这里就是把刚刚后端给我们返回的带有openid的摘取出来了 并转换成JSON 用到了qs的库</span></span><br><span class="line">    <span class="keyword">const</span> qs = Qs.parse(<span class="built_in">window</span>.location.search.substr(<span class="number">1</span>))</span><br><span class="line">    <span class="keyword">const</span> openid = qs.opind</span><br><span class="line">    <span class="comment">//然后带着ID去像后端发送请求</span></span><br><span class="line">    <span class="keyword">const</span> ret = <span class="keyword">await</span> axios.get(<span class="string">&#x27;/getUser&#x27;</span>,&#123;<span class="attr">params</span>:&#123;openid&#125;&#125;)</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&#x27;User&#x27;</span>,ret.data)</span><br><span class="line">&#125; </span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//后端接受请求 去掉微信服务器的接口 带着你的iD去调用</span></span><br><span class="line">router.get(<span class="string">&#x27;/getUser&#x27;</span>, <span class="keyword">async</span>( ctx,next) =&gt; &#123;</span><br><span class="line">    <span class="comment">//获取到前端传过来的ID</span></span><br><span class="line">    <span class="keyword">const</span> openid = ctx.query.openid</span><br><span class="line">    <span class="built_in">console</span>.log(openid)</span><br><span class="line">    <span class="comment">//去向微信服务器发送获取用户信息的请求</span></span><br><span class="line">    <span class="keyword">const</span> userInfo = <span class="keyword">await</span> oauth.getUser(openid)</span><br><span class="line">    <span class="built_in">console</span>.log(userInfo)</span><br><span class="line">    <span class="comment">//返回给前端</span></span><br><span class="line">    ctx.body = userInfo</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
<h2 id="3-微信JSSDK"><a href="#3-微信JSSDK" class="headerlink" title="3.微信JSSDK"></a>3.微信JSSDK</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>前端首先要配置jsskd的库  主要是要获取jsconfig 用下面的去配置</span><br><span class="line">&lt;script src=<span class="string">&quot;http://res.wx.qq.com/open/js/jweixin-1.4.0.js&quot;</span>&gt;&lt;/script&gt;</span><br><span class="line"><span class="number">2.</span>加载之后 就会有wx的这个对象 这个对象就是用来调取你手机上的一些功能</span><br><span class="line"><span class="number">3.</span>首先我们后端服务器 要去微信的服务器获取一下jssdk 这里我们使用了co-wechat-api的包</span><br><span class="line"><span class="number">4.</span>获取之后是一个配置文件 我们需要把他返回给前端 </span><br><span class="line"><span class="number">5.</span>然后前端来配置到我们上面的那个加载的wx的库里 这个过程是微信验证我们 </span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"> <span class="keyword">async</span> <span class="function"><span class="title">getJSConfig</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">     <span class="comment">//前端去发送给后端请求  然后后端接收到之后 去微信服务器发送请求 请求回来的结果就是jssdk的配置文件前端jweixin的库去把那个jssdk的配置文件加载进去 就可以调取手机的一些接口了</span></span><br><span class="line">    <span class="keyword">const</span> res = <span class="keyword">await</span> axios.get(<span class="string">&#x27;/getJSConfig&#x27;</span>,&#123;<span class="attr">params</span>:&#123;<span class="attr">url</span>:<span class="built_in">window</span>.location.href&#125;&#125;)</span><br><span class="line">    <span class="built_in">console</span>.log(res)</span><br><span class="line">    res.data.jsApilist = []</span><br><span class="line">     <span class="comment">//把后端的返回的jsskd文件返配置进去</span></span><br><span class="line">    wx.config(res.data)</span><br><span class="line">     <span class="comment">//这里就可以调取手机的一些东西了 具体接口查看文档</span></span><br><span class="line">    wx.chooseImage(&#123;</span><br><span class="line">    count: <span class="number">1</span>, <span class="comment">// 默认9</span></span><br><span class="line">    sizeType: [<span class="string">&#x27;original&#x27;</span>, <span class="string">&#x27;compressed&#x27;</span>], <span class="comment">// 可以指定是原图还是压缩图，默认二者都有</span></span><br><span class="line">    sourceType: [<span class="string">&#x27;album&#x27;</span>, <span class="string">&#x27;camera&#x27;</span>], <span class="comment">// 可以指定来源是相册还是相机，默认二者都有</span></span><br><span class="line">    success: <span class="function"><span class="keyword">function</span> (<span class="params">res</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">var</span> localIds = res.localIds; <span class="comment">// 返回选定照片的本地ID列表，localId可以作为img标签的src属性显示图片</span></span><br><span class="line">    &#125;</span><br><span class="line">    &#125;);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//后端 去微信服务器请求之前 需要先配置好我们的ID 和scrset</span></span><br><span class="line"><span class="comment">//我们要用到这个包 其实已经把一些去微信请求的接口已经给我们封装好了</span></span><br><span class="line"><span class="keyword">const</span> api = <span class="keyword">new</span> WechatAPI(conf.appid,conf.appsecret)</span><br><span class="line"><span class="comment">//接收前台的请求 去请求微信服务器 然后 返还给前端</span></span><br><span class="line">router.get(<span class="string">&#x27;/getJSConfig&#x27;</span>,<span class="keyword">async</span>(ctx,next) =&gt; &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(api,<span class="string">&#x27;-----api&#x27;</span>)</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&#x27;getJSSDK....&#x27;</span>,ctx.query)</span><br><span class="line">    <span class="keyword">const</span> res = <span class="keyword">await</span> api.getJsConfig(ctx.query)</span><br><span class="line">    <span class="built_in">console</span>.log(res,<span class="string">&#x27;------&#x27;</span>)</span><br><span class="line">    ctx.body = res</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
</article><div class="post-copyright"><div class="post-copyright__title"><span class="post-copyright-info"><h>微信公众号配置</h></span></div><div class="post-copyright__type"><span class="post-copyright-info"><a href="http://example.com/2020/04/20/%E5%85%AC%E4%BC%97%E5%8F%B7%E9%85%8D%E7%BD%AE/">http://example.com/2020/04/20/%E5%85%AC%E4%BC%97%E5%8F%B7%E9%85%8D%E7%BD%AE/</a></span></div><div class="post-copyright-m"><div class="post-copyright-m-info"><div class="post-copyright-a"><h>作者</h><div class="post-copyright-cc-info"><h>北城韩雨</h></div></div><div class="post-copyright-c"><h>发布于</h><div class="post-copyright-cc-info"><h>2020-04-20</h></div></div><div class="post-copyright-u"><h>更新于</h><div class="post-copyright-cc-info"><h>2019-06-03</h></div></div><div class="post-copyright-c"><h>许可协议</h><div class="post-copyright-cc-info"><a class="icon" rel="noopener" target="_blank" title="Creative Commons" href="https://creativecommons.org/"><i class="fab fa-creative-commons"></i></a><a rel="noopener" target="_blank" title="CC BY-NC-SA 4.0" href="https://creativecommons.org/licenses/by-nc-sa/4.0/">CC BY-NC-SA 4.0</a></div></div></div></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/%E5%BE%AE%E4%BF%A1%E5%85%AC%E4%BC%97%E5%8F%B7%E9%85%8D%E7%BD%AE/">微信公众号配置</a></div><div class="post_share"></div></div><link rel="stylesheet" href="/" media="defer" onload="this.media='all'"/><div class="post-reward"><button class="tip-button reward-button"><span class="tip-button__text">打赏</span><div class="coin-wrapper"><div class="coin"><div class="coin__middle"></div><div class="coin__back"></div><div class="coin__front"></div></div></div><div class="reward-main"><ul class="reward-all"><li class="reward-item"><a href="/img/weixin.jpg" target="_blank"><img class="post-qr-code-img" src="/img/weixin.jpg" alt="微信"/></a><div class="post-qr-code-desc">微信</div></li><li class="reward-item"><a href="/img/taobao.jpg" target="_blank"><img class="post-qr-code-img" src="/img/taobao.jpg" alt="支付宝"/></a><div class="post-qr-code-desc">支付宝</div></li></ul></div></button></div><script defer="defer" src="/"></script><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2020/04/21/uni-app/"><img class="prev-cover" src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2017-07-20%2F4f06f9aa2ebcea6691a78537052e8e9f%2F00001.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&amp;refer=http%3A%2F%2Fpic.vjshi.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1629530599&amp;t=32fb97bad7007bdc6c712ccadacd5721" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">Uni-app</div></div></a></div><div class="next-post pull-right"><a href="/2020/04/18/%E7%AE%97%E6%B3%95/"><img class="next-cover" src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2016-12-29%2F0604092c7129acd0f4029c6728db7fd7%2F00001.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&amp;refer=http%3A%2F%2Fpic.vjshi.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1629530724&amp;t=333c5056a813a542419e5b661ebcfcd9" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">常用算法及思路</div></div></a></div></nav></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="/img/my.jpg" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">北城韩雨</div><div class="author-info__description">一个前端界的小学生</div></div><div class="card-info-data"><div class="card-info-data-item is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">65</div></a></div><div class="card-info-data-item is-center"><a href="/tags/"><div class="headline">标签</div><div class="length-num">23</div></a></div><div class="card-info-data-item is-center"><a href="/categories/"><div class="headline">分类</div><div class="length-num">21</div></a></div></div><a class="button--animated" id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/wazer1987"><i class="fab fa-github"></i><span>Follow Me</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="/wangzhengvslike@163.com" target="_blank" title="Email"><i class="fas fa-envelope"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn card-announcement-animation"></i><span>公告</span></div><div class="announcement_content"><p>感谢访问本站，如喜欢请收藏。本站主要分享前端知识，立志成为资深前端工程师，但目前是一个前端界的小学生 若喜欢可以打赏请博主喝一杯冰阔落</p> <p>另外请大家多多支持淼哥的开源项目</p> <p>https://github.com/flipped-aurora/gin-vue-admin</p> <p>🌟🌟欢迎大家start 🌟🌟</p> <p>欢迎加入博主的前端技术交流群</p> <p style="background:rgba(255,255,255,1);margin:0"> <img src="/img/code.png"></img> </p></div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%85%AC%E4%BC%97%E5%8F%B7%E6%9C%8D%E5%8A%A1%E7%AB%AF"><span class="toc-text">公众号服务端</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#1-%E6%9C%AC%E5%9C%B0%E7%AB%AF%E5%8F%A3%E6%98%A0%E5%B0%84%E5%85%AC%E7%BD%91IP"><span class="toc-text">1.本地端口映射公网IP</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-%E5%85%AC%E4%BC%97%E5%8F%B7%E6%B5%8B%E8%AF%95%E9%85%8D%E7%BD%AE"><span class="toc-text">2.公众号测试配置</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-%E6%9C%8D%E5%8A%A1%E5%99%A8%E4%B9%8B%E9%97%B4%E4%BA%A4%E4%BA%92"><span class="toc-text">3.服务器之间交互</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4-%E6%9C%8D%E5%8A%A1%E5%99%A8API%E8%B0%83%E7%94%A8"><span class="toc-text">4.服务器API调用</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#5-%E5%85%A8%E5%B1%80%E7%A5%A8%E6%8D%AE"><span class="toc-text">5.全局票据</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%85%AC%E4%BC%97%E5%8F%B7%E7%BD%91%E9%A1%B5%E7%AB%AF"><span class="toc-text">公众号网页端</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#1-OAuth2%E6%8E%88%E6%9D%83"><span class="toc-text">1.OAuth2授权</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-%E8%8E%B7%E5%8F%96%E7%94%A8%E6%88%B7%E4%BF%A1%E6%81%AF"><span class="toc-text">2.获取用户信息</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-%E5%BE%AE%E4%BF%A1JSSDK"><span class="toc-text">3.微信JSSDK</span></a></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/2021/04/16/gin-vue-admin/" title="Gin-Vue-Admin"><img src="/img/gin-vue-admin.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Gin-Vue-Admin"/></a><div class="content"><a class="title" href="/2021/04/16/gin-vue-admin/" title="Gin-Vue-Admin">Gin-Vue-Admin</a><time datetime="2021-04-15T16:00:00.000Z" title="发表于 2021-04-16 00:00:00">2021-04-16</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/01/08/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E7%BB%84%E4%BB%B6%E7%9A%84%E6%B8%B2%E6%9F%9310/" title="Vue源码开发-组件的渲染10"><img src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2018-07-17%2F3eca5394963b50e37b7be2ba320029ec%2F00003.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&amp;refer=http%3A%2F%2Fpic.vjshi.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1629530644&amp;t=10679b5732c52e39eeb588040240a790" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Vue源码开发-组件的渲染10"/></a><div class="content"><a class="title" href="/2021/01/08/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E7%BB%84%E4%BB%B6%E7%9A%84%E6%B8%B2%E6%9F%9310/" title="Vue源码开发-组件的渲染10">Vue源码开发-组件的渲染10</a><time datetime="2021-01-07T16:00:00.000Z" title="发表于 2021-01-08 00:00:00">2021-01-08</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/01/07/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E7%BB%84%E4%BB%B6%E7%9A%84%E5%90%88%E5%B9%B6%E7%AD%96%E7%95%A509/" title="Vue源码开发-组件合并策略09"><img src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fcz-video-photo.oss-cn-beijing.aliyuncs.com%2F20191017%2F4cfc341591d242327b67e9ceb6953612_00002.jpg&amp;refer=http%3A%2F%2Fcz-video-photo.oss-cn-beijing.aliyuncs.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1629530702&amp;t=80943e12d5239f63beafd6d1c540855f" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Vue源码开发-组件合并策略09"/></a><div class="content"><a class="title" href="/2021/01/07/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E7%BB%84%E4%BB%B6%E7%9A%84%E5%90%88%E5%B9%B6%E7%AD%96%E7%95%A509/" title="Vue源码开发-组件合并策略09">Vue源码开发-组件合并策略09</a><time datetime="2021-01-06T16:00:00.000Z" title="发表于 2021-01-07 00:00:00">2021-01-07</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/01/06/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E6%95%B0%E7%BB%84%E7%9A%84%E4%BE%9D%E8%B5%96%E6%94%B6%E9%9B%8608/" title="Vue源码开发-数组的依赖收集08"><img src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2018-07-17%2F3eca5394963b50e37b7be2ba320029ec%2F00003.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&amp;refer=http%3A%2F%2Fpic.vjshi.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1629530644&amp;t=10679b5732c52e39eeb588040240a790" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Vue源码开发-数组的依赖收集08"/></a><div class="content"><a class="title" href="/2021/01/06/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E6%95%B0%E7%BB%84%E7%9A%84%E4%BE%9D%E8%B5%96%E6%94%B6%E9%9B%8608/" title="Vue源码开发-数组的依赖收集08">Vue源码开发-数组的依赖收集08</a><time datetime="2021-01-05T16:00:00.000Z" title="发表于 2021-01-06 00:00:00">2021-01-06</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/01/06/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F%E5%90%88%E5%B9%B607/" title="Vue源码开发-生命周期合并07"><img src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2018-07-17%2Faf41dabad727ba80c9ca8a2bfbd22a58%2F00004.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&amp;refer=http%3A%2F%2Fpic.vjshi.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1629530659&amp;t=bbcfe1e86f393eecbb0722db1b92e475" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Vue源码开发-生命周期合并07"/></a><div class="content"><a class="title" href="/2021/01/06/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F%E5%90%88%E5%B9%B607/" title="Vue源码开发-生命周期合并07">Vue源码开发-生命周期合并07</a><time datetime="2021-01-05T16:00:00.000Z" title="发表于 2021-01-06 00:00:00">2021-01-06</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright"><span>&copy;2017 - 2021</span><svg style="width:2em; height:2em;position: relative;top: 10px;" aria-hidden="true"><use xlink:href="#icon-Butterfly"></use></svg><span>北城韩雨</span></div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div><div class="footer_custom_text"><p><a style="margin-inline:5px" target="_blank" href="https://hexo.io/"><img src="https://img.shields.io/badge/Frame-Hexo-blue?style=flat&logo=hexo" title="博客框架为Hexo"></a><a style="margin-inline:5px" target="_blank" href="https://butterfly.js.org/"><img src="https://img.shields.io/badge/Theme-Butterfly-6513df?style=flat&logo=bitdefender" title="主题采用butterfly"></a><a style="margin-inline:5px" target="_blank" href="https://www.jsdelivr.com/"><img src="https://img.shields.io/badge/CDN-jsDelivr-orange?style=flat&logo=jsDelivr" title="本站使用JsDelivr为静态资源提供CDN加速"></a></p></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/medium-zoom/dist/medium-zoom.min.js"></script><div class="js-pjax"></div><script defer src="/live2d-widget/autoload.js"></script><script src="/js/coin.js"></script><script async src="//at.alicdn.com/t/font_2700632_3rtvhzzpl3h.js"></script><script src="/js/liuxing.js" data-pjax></script><canvas class="fireworks" mobile="false"></canvas><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/fireworks.min.js"></script><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/activate-power-mode.min.js"></script><script>POWERMODE.colorful = true;
POWERMODE.shake = true;
POWERMODE.mobile = false;
document.body.addEventListener('input', POWERMODE);
</script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div><!-- hexo injector body_end start --><div class="js-pjax"><script async="async">var arr = document.getElementsByClassName('recent-post-item');
for(var i = 0;i<arr.length;i++){
    arr[i].classList.add('wow');
    arr[i].classList.add('animate__zoomIn');
    arr[i].setAttribute('data-wow-duration', '1s');
    arr[i].setAttribute('data-wow-delay', '0ms');
    arr[i].setAttribute('data-wow-offset', '100');
    arr[i].setAttribute('data-wow-iteration', '1');
  }</script><script async="async">var arr = document.getElementsByClassName('card-widget');
for(var i = 0;i<arr.length;i++){
    arr[i].classList.add('wow');
    arr[i].classList.add('animate__zoomIn');
    arr[i].setAttribute('data-wow-duration', '');
    arr[i].setAttribute('data-wow-delay', '');
    arr[i].setAttribute('data-wow-offset', '');
    arr[i].setAttribute('data-wow-iteration', '');
  }</script></div><script defer src="https://cdn.jsdelivr.net/gh/graingert/wow@1.3.0/dist/wow.min.js"></script><script defer src="https://cdn.jsdelivr.net/npm/hexo-butterfly-wowjs/lib/wow_init.min.js"></script><script async src="//at.alicdn.com/t/font_2032782_8d5kxvn09md.js"></script><!-- hexo injector body_end end --></body></html>